package com.scitc.pleasurevideo.data;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 853
 */
public class DataUtil {

    /***
     * String[] 转 List<String>
     * @param array
     * @return List<String>
     * List<String> list = Arrays.asList(array);
     */
    public static List<String> array_list(String[] array) {
        List<String> items = new LinkedList<>();
        for (String string : array) {
            items.add(string);
        }
        array = null;
        return items;
    }

    /***
     * List<String> 转 String[]
     * @param items
     * @return String[]
     * String[] arr = list.toArray(new String[list.size()]);
     */
    public static String[] list_array(List<String> items) {
        String[] array = new String[items.size()];
        for (int i = 0; i < items.size(); i++) {
            array[i] = items.get(i);
        }
        items.clear();
        return array;
    }

    /**
     * String[] 转 String
     *
     * @param array
     * @return String
     * String result=String.join(",", strings);
     */
    public static String array_string(String[] array) {
        StringBuilder string = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i == array.length - 1) {
                string.append(array[i]);
            } else {
                string.append(array[i]).append(",");
            }
        }
        return string.toString();
    }

    /**
     * List<String> 转 String
     *
     * @param items
     * @return String
     */
    public static String list_string(List<String> items) {
        StringBuilder string = new StringBuilder();
        for (int i = 0; i < items.size(); i++) {
            if (i == items.size() - 1) {
                string.append(items.get(i));
            } else {
                string.append(items.get(i)).append(",");
            }
        }
        return string.toString();
    }

    /**
     * 判断在srcstr中 substr出现几次
     *
     * @param srcStr
     * @param subStr
     * @return
     */
    public static int countOfSubstr(String srcStr, String subStr) {
        int count = 0;
        if (srcStr.contains(subStr)) {     //只有srcStr中包含subStr时，才做以下的分隔处理，否则直接返回0;
            //防止subStr出现在srcStr的首尾位置，分别在源字符串srcStr的首尾加上空格
            String _srcStr = "    " + srcStr + "   ";
            String[] strs = _srcStr.split(subStr);
            count = strs.length - 1;

        } else {
            return 0;
        }
        return count;
    }


    /**
     * get App versionName
     *
     * @param context
     * @return
     */
    public static String getVersionName(Context context) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packageInfo;
        String versionName = "";
        try {
            packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

}
